﻿using SmartHotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartHotel.DAL
{
    //HotelContext.RoomTypes
    public class RoomTypeDAL
    {
        public RoomTypeDAL(HotelContext hotelContext) {
            HotelContext = hotelContext;
        }

        public HotelContext HotelContext { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="roomType"></param>
        /// <returns></returns>
        public int AddRoomType(RoomType roomType) { 
            HotelContext.RoomTypes.Add(roomType);
            return HotelContext.SaveChanges(); //   
        }
        //查询
        public RoomType GetRoomTypeById(int id) {
            
            //
            return HotelContext.RoomTypes.Where(x=>x.Id==id).FirstOrDefault();
        }

        public int Delete(int id) {
            var roomType1 = GetRoomTypeById(id);//要删除的房型对象
            if (roomType1 != null) {
                HotelContext.RoomTypes.Remove(roomType1);
            }
            return HotelContext.SaveChanges();
        }
        //更新
        public int Update(RoomType roomType) {

            var roomType1 = GetRoomTypeById(roomType.Id);
            if (roomType1 != null)
            {
                roomType1.Name  = roomType.Name;
                roomType1.ImgUrl = roomType.ImgUrl;
            }
            return HotelContext.SaveChanges();
        }
        /// <summary>
        /// 获取所有房型
        /// </summary>
        /// <returns></returns>
        public List<RoomType> GetRoomTypes() {
            //return HotelContext.RoomTypes.ToList(); 
            var roomTypes = HotelContext.RoomTypes.AsQueryable();
            return roomTypes.ToList();
        }
    }
}
